package com.mbc.web.common.client.ui;

import java.util.Date;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.mbc.web.common.client.ClientSingleton;
import com.mbc.web.common.client.CommonControllerMgr;
import com.mbc.web.common.client.ViewUtils;

public class ContentTag extends Composite {

	@UiField HTMLPanel g_content;
	private SessionTimer sessionTimer;
	private Date lastestDateTime = new Date();
	private long timeInMillisecond;
	private int interval = 300000;
	
	private class SessionTimer extends Timer{

		@Override
		public void run() {
			
			lastestDateTime.setTime(timeInMillisecond);
			CommonControllerMgr.heartBeat(lastestDateTime, heartBeatCallback());
			timeInMillisecond += interval;
		}
		
	}
	
	private AsyncCallback<Integer> heartBeatCallback(){
		return new AsyncCallback<Integer>() {
			
			@Override
			public void onSuccess(Integer result) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		};
	}
	
	public static boolean isViewLoaded = false;
	
	private static ContentTagUiBinder uiBinder = GWT
			.create(ContentTagUiBinder.class);

	interface ContentTagUiBinder extends UiBinder<Widget, ContentTag> {
	}

	public ContentTag() {
		initWidget(uiBinder.createAndBindUi(this));
		sessionTimer = new SessionTimer();
		sessionTimer.scheduleRepeating(interval);
		timeInMillisecond = lastestDateTime.getTime();
		this.getElement().setId("content_div");
		
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			
			@Override
			public void execute() {
				sessionTimer.run();
			}
		});
	}
	
	public void showHome(Composite view){
		g_content.clear();
		g_content.add(view);
	}

	public void showView(Composite view)
	{
		g_content.clear();
		g_content.add(view);
		ViewUtils.executeAccessControl(ClientSingleton.currentAppItem.getAccessCodes());
		ViewUtils.attachBlurEvent();
		ViewUtils.prepareTabindexNEnter();
		ViewUtils.scrollerbar();
		isViewLoaded = true;
	}
}
